package com.crr.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.crr.demo.bean.User;
import com.crr.demo.common.ResultModel;
import com.crr.demo.impl.service.UserService;
import com.crr.demo.utils.DateUtils;
import com.crr.demo.utils.JWTUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;


@RestController
public class AdminController {
    @Resource
    UserService userService;
    /**
     * 获取用户信息
     */
    @GetMapping("/user")
    public ResultModel getUserOfLogin(HttpServletRequest request) throws UnsupportedEncodingException {
        //获取Headers中的参数
        String token = request.getHeader("Authorization");
        User user = userService.getUser(JWTUtils.verify(token));
        JSONObject jsonObject  = new JSONObject();
        JSON.toJSONStringZ(jsonObject, SerializeConfig.getGlobalInstance(), SerializerFeature.QuoteFieldNames);
        jsonObject.put("name",user.getName());
        jsonObject.put("role",user.getRole());
        return new ResultModel(jsonObject,100, DateUtils.getTime());
    }
}
